package com.ruoyi.project.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.member.domain.MessageSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ProjectRiskMapper;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.service.IProjectRiskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目风险Service业务层处理
 *
 * @author yuhaoyang
 * @date 2024-06-04
 */
@Service
public class ProjectRiskServiceImpl implements IProjectRiskService {
    @Autowired
    private ProjectRiskMapper projectRiskMapper;

    /**
     * 查询项目风险
     *
     * @param riskId 项目风险主键
     * @return 项目风险
     */
    @Override
    public ProjectRisk selectProjectRiskByRiskId(Long riskId) {
        return projectRiskMapper.selectProjectRiskByRiskId(riskId);
    }

    /**
     * 查询项目风险列表
     *
     * @param projectRisk 项目风险
     * @return 项目风险
     */
    @Override
    public List<ProjectRisk> selectProjectRiskList(ProjectRisk projectRisk) {
        return projectRiskMapper.selectProjectRiskList(projectRisk);
    }

    /**
     * 新增项目风险
     *
     * @param projectRisk 项目风险
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int insertProjectRisk(ProjectRisk projectRisk) {
        sendMessage(projectRisk);
        return projectRiskMapper.insertProjectRisk(projectRisk);
    }

    /**
     * 修改项目风险
     *
     * @param projectRisk 项目风险
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int updateProjectRisk(ProjectRisk projectRisk) {
        sendMessage(projectRisk);
        return projectRiskMapper.updateProjectRisk(projectRisk);
    }

    /**
     * 批量删除项目风险
     *
     * @param riskIds 需要删除的项目风险主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectRiskByRiskIds(Long[] riskIds) {
        return projectRiskMapper.deleteProjectRiskByRiskIds(riskIds);
    }

    /**
     * 删除项目风险信息
     *
     * @param riskId 项目风险主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectRiskByRiskId(Long riskId) {
        return projectRiskMapper.deleteProjectRiskByRiskId(riskId);
    }

    /**
     * 校验风险标题唯一性
     *
     * @param projectRisk 项目风险
     * @return 结果
     */
    @Override
    public boolean checkUniqueRiskTitle(ProjectRisk projectRisk) {
        return projectRiskMapper.checkUniqueRiskTitle(projectRisk) != null;
    }

    /**
     * 发送消息
     *
     * @param projectRisk 工单
     * @return 结果
     */
    public int sendMessage(ProjectRisk projectRisk) {
        MessageSend messageSend = new MessageSend();
        String msg = "项目风险：" + projectRisk.getRiskTitle() + "，风险内容：" + projectRisk.getRiskContent();
        messageSend.setProjectId(projectRisk.getProjectId());
        messageSend.setMessageContent(msg);
        messageSend.setMessageDate(new Date());
        // 获取当前登录用户
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        messageSend.setUserId(user.getUserId());
        projectRiskMapper.insertSend(messageSend);
        projectRiskMapper.insertReceive(messageSend.getMessageId(), projectRisk.getProjectId(), projectRisk.getUserId());
        return 1;
    }
}
